// 剪映1面题目
function wait() {
  return new Promise((resolve, rejcet) => {
    setTimeout(resolve, 10 * 1000);
  });
}

async function main() {
  console.time();
  const x = wait();
  const y = wait();
  const z = wait();
  await x;
  await y;
  await z;
  await Promise.all([x, y, z]);
  console.timeEnd();
}

main(); // 间隔10s

console.log(typeof null); // Object
console.log(null instanceof Object); // false
// 无序数组  list = [7,2,3,5,11,12,2,4,17,9,13];
// 输出连续区间 ['2->5','7','9','11->13','17',]
const arr = [7, 2, 3, 5, 11, 12, 2, 12, 4, 17, 9, 13];
function fn(arr) {
  const res = [];
  // 排序 去重  用到双指针
  arr.sort((a, b) => a - b);
  const set = new Set(arr);
  const list = [...set];
  let start = list[0];
  let end = list[0];
  console.log(list);
  for (let i = 1; i < list.length; i++) {
    if (list[i] === end + 1) {
      end = list[i];
    } else {
      if (start !== end) {
        res.push(`${start}->${end}`);
      } else {
        res.push(start.toString());
      }
      start = list[i];
      end = list[i];
    }
  }

  if (start === end) {
    res.push(start.toString());
  } else {
    res.push(`${start}->${end}`);
  }
  return res;
}

console.log(fn(arr));

// 这种方法好理解
const arr2 = [7, 2, 3, 5, 11, 12, 2, 12, 4, 17, 9, 13];
function fn(arr) {
  const res = [];
  arr.sort((a, b) => a - b);
  let start = arr[0];
  let end = arr[0];
  // 从0开始
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === arr[i + 1]) continue;
    if (arr[i] + 1 === arr[i + 1]) {
      end = arr[i + 1];
    } else {
      if (start !== end) {
        res.push(`${start}->${end}`);
      } else {
        res.push(`${start}`);
      }
      start = arr[i + 1];
      end = arr[i + 1];
    }
  }
  return res;
}

console.log(fn(arr2));

